//
//  ViewController.m
//  InterceptUrl
//
//  Created by WSXiong on 2017/10/11.
//  Copyright © 2017年 CMCC_CMSZ. All rights reserved.
//

#import "ViewController.h"
#import "UIWebViewVC.h"
#import "WKWebViewVC.h"

#define WIDTH    self.view.frame.size.width

@interface ViewController ()<UITextFieldDelegate>

@property (nonatomic ,copy) NSString *str;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(20, 230, WIDTH - 40, 40)];
    tf.backgroundColor = [UIColor whiteColor];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    tf.font = [UIFont systemFontOfSize:14];
    tf.textColor = [UIColor blackColor];
    tf.clearButtonMode = UITextFieldViewModeAlways;
    tf.delegate = self;
    [self.view addSubview:tf];
    
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(100, 300, WIDTH - 200, 40);
    [btn1 setTitle:@"UIWebView拦截" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor lightGrayColor];
    btn1.titleLabel.font = [UIFont systemFontOfSize:15];
    btn1.tag = 1;
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectMake(100, 350, WIDTH - 200, 40);
    [btn2 setTitle:@"WKWebView拦截" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn2.backgroundColor = [UIColor lightGrayColor];
    btn2.titleLabel.font = [UIFont systemFontOfSize:15];
    btn2.tag = 2;
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)action:(UIButton *)btn{
    if (_str.length == 0) {
        return;
    }
    if (btn.tag == 1) {
        UIWebViewVC *vc = [[UIWebViewVC alloc]init];
        vc.inputUrlStr = self.str;
        [self presentViewController:vc animated:YES completion:nil];
    }else{
        WKWebViewVC *vc = [[WKWebViewVC alloc]init];
        vc.inputUrlStr = self.str;
        [self presentViewController:vc animated:YES completion:nil];
    }
}

#pragma mark- UItextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    self.str = textField.text;
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
